// 文章相关类型定义，与后端表结构（articles）对齐
// 允许前端在不同层面使用统一的类型并兼容蛇形 / 驼峰字段

export type ArticleStatus = 'DRAFT' | 'PUBLISHED' | 'ARCHIVED'

export type ArticleCategory =
  | 'RECOMMEND'
  | '心理咨询'
  | '缓解焦虑'
  | '情感危机'
  | '改善人际'
  | '个人成长'

// 后端原始记录（可能是蛇形命名）
export interface ArticleRecordRaw {
  id: number
  title: string
  author_id?: number
  authorId?: number
  author_name?: string
  authorName?: string
  cover_image?: string | null
  coverImage?: string | null
  description?: string | null
  content?: string | null
  category?: ArticleCategory | string | null
  tags?: any // 数组或 JSON 字符串
  view_count?: number
  viewCount?: number
  favorite_count?: number
  favoriteCount?: number
  status?: ArticleStatus
  created_at?: string
  createdAt?: string
  updated_at?: string
  updatedAt?: string
}

// 前端归一化后的文章（正文）
export interface Article extends ArticleSummary {
  content?: string | null
  status?: ArticleStatus
  tags?: string[]
}

// 列表用精简信息
export interface ArticleSummary {
  id: number
  title: string
  authorId: number
  authorName?: string
  coverImage?: string | null
  description?: string | null
  category?: ArticleCategory | string | null
  viewCount: number
  favoriteCount: number
  createdAt?: string
  updatedAt?: string
}

export interface ArticleListQuery {
  page?: number
  size?: number
  category?: string
  keyword?: string
}

export interface ArticleListResp {
  list: ArticleSummary[]
  total: number
}

// 归一化工具（可供 API 层调用）
export function normalizeArticle(raw: ArticleRecordRaw): Article {
  return {
    id: raw.id,
    title: raw.title,
    authorId: raw.author_id ?? raw.authorId ?? 0,
    authorName: raw.author_name ?? raw.authorName,
    coverImage: raw.cover_image ?? raw.coverImage ?? null,
    description: raw.description ?? null,
    category: raw.category ?? null,
    viewCount: raw.view_count ?? raw.viewCount ?? 0,
    favoriteCount: raw.favorite_count ?? raw.favoriteCount ?? 0,
    createdAt: raw.created_at ?? raw.createdAt,
    updatedAt: raw.updated_at ?? raw.updatedAt,
    status: raw.status,
    content: raw.content ?? null,
    tags: normalizeTags(raw.tags),
  }
}

export function normalizeArticleSummary(raw: ArticleRecordRaw): ArticleSummary {
  return {
    id: raw.id,
    title: raw.title,
    authorId: raw.author_id ?? raw.authorId ?? 0,
    authorName: raw.author_name ?? raw.authorName,
    coverImage: raw.cover_image ?? raw.coverImage ?? null,
    description: raw.description ?? null,
    category: raw.category ?? null,
    viewCount: raw.view_count ?? raw.viewCount ?? 0,
    favoriteCount: raw.favorite_count ?? raw.favoriteCount ?? 0,
    createdAt: raw.created_at ?? raw.createdAt,
    updatedAt: raw.updated_at ?? raw.updatedAt,
  }
}

function normalizeTags(tags: any): string[] {
  if (!tags) return []
  if (Array.isArray(tags)) return tags.map(String)
  if (typeof tags === 'string') {
    try {
      const parsed = JSON.parse(tags)
      return Array.isArray(parsed) ? parsed.map(String) : []
    } catch {
      // 逗号分隔的简单格式
      return tags.split(',').map(s => s.trim()).filter(Boolean)
    }
  }
  return []
}
